package com.compty.security.domain;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.compty.paint.domain.CityInfo;
import com.compty.paint.domain.ProvinceInfo;
import com.compty.paint.util.Constants;


/**
 * @Todo:注册登录信息
 * @author: zhuqh
 * @CreateTime:2011-6-1 上午09:18:26
 */
@Entity
@Table(name = "T_USER")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User implements java.io.Serializable{

	private static final long serialVersionUID = 4450030412468672979L;
	
	private Long id;
	
	/************ 登录用户名  ****************/
	private String username;
	
	/************ 注册用户所属类别 ****************/
	private String userType;//personal ,company
	
	/************ 公司名称 ****************/
	private String companyName;
	
	/************ 手机 ****************/
	private String mobile;
	
	/************ 固定电话 ****************/
	private String telphone;
	
	/************ 邮件地址 ****************/
	private String emailAddress;
	
	/************ 通讯地址 ****************/
	private String address;
	
	private String yyzh;//营业执照注册号
	private String idCard;//身份证号
	private String zipCode;
	private ProvinceInfo provinceInfo;
	private CityInfo cityInfo;
	private String contact;//业务联系人
	private String tel;//联系电话
	private String account;//开户名
	private String bank;//银行
	private String account_number;//账号
	
	/************ 密码 ****************/
	private String password;
	
	/************ 注册时间 ****************/
	private Date registerTime;
	
	/************ 注册状态 ****************/
	private UserState userState;
	
	 /** * descn. */
    private String descn;
    
    /***是否为管理员 1表示管理员,0为非管理员***/
    private String administratorFlag;
    
	  /** * roles. */
    private Set<Role> roles = new HashSet<Role>(0);
    
    private String roleName;

	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(name = "username",length=30)
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "usertype", length = 20)
	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	@Column(name = "companyname", length = 50)
	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	@Column(name = "mobile", length = 20)
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	@Column(name = "telphone", length = 20)
	public String getTelphone() {
		return telphone;
	}

	public void setTelphone(String telphone) {
		this.telphone = telphone;
	}

	@Column(name = "email_address", length = 50)
	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	@Column(name = "address")
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Column(name = "password", length = 50)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	

	@Column(name = "register_time")
	public Date getRegisterTime() {
		return registerTime;
	}

	public void setRegisterTime(Date registerTime) {
		this.registerTime = registerTime;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name="userstate_id")
	public UserState getUserState() {
		return userState;
	}

	public void setUserState(UserState userState) {
		this.userState = userState;
	}
	
	 @ManyToMany(cascade =  {
		        CascadeType.PERSIST, CascadeType.MERGE}
		    , fetch = FetchType.LAZY)
		    @JoinTable(name = "T_USER_ROLE", joinColumns =  {
		        @JoinColumn(name = "USER_ID")
		    }
		    , inverseJoinColumns =  {
		        @JoinColumn(name = "ROLE_ID")
		    }
		    )
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	
	@Column(name = "DESCN", length = 200)
	public String getDescn() {
		return descn;
	}

	public void setDescn(String descn) {
		this.descn = descn;
	}
	
	 /** * @return 当前用户是否可用. */
    @Transient
    public boolean isEnabled() {
        return ((userState != null) && Constants.User_State_Normal.equals(userState.getEnglish_name()));
    }

    /** * @param enabled boolean. */
    public void setEnabled(boolean enabled) {
        if (enabled) {
        	userState.setEnglish_name(Constants.User_State_Normal);
        } else {
        	userState.setEnglish_name(Constants.User_State_hidden);
        }
    }

    @Column(name = "administrator_flag", length=10)
	public String getAdministratorFlag() {
		return administratorFlag;
	}

	public void setAdministratorFlag(String administratorFlag) {
		this.administratorFlag = administratorFlag;
	}

	@Column(name = "yyzh", length = 50)
	public String getYyzh() {
		return yyzh;
	}

	public void setYyzh(String yyzh) {
		this.yyzh = yyzh;
	}

	@Column(name = "idcard", length = 50)
	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}

	@Column(name = "zipcode", length = 50)
	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="province_id")
	public ProvinceInfo getProvinceInfo() {
		return provinceInfo;
	}

	public void setProvinceInfo(ProvinceInfo provinceInfo) {
		this.provinceInfo = provinceInfo;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="city_id")
	public CityInfo getCityInfo() {
		return cityInfo;
	}

	public void setCityInfo(CityInfo cityInfo) {
		this.cityInfo = cityInfo;
	}

	@Column(name = "contact",length = 100)
	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	@Column(name = "tel",length = 50)
	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	@Column(name = "account",length = 100)
	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	@Column(name = "bank",length = 100)
	public String getBank() {
		return bank;
	}

	public void setBank(String bank) {
		this.bank = bank;
	}

	@Column(name = "account_number",length = 100)
	public String getAccount_number() {
		return account_number;
	}

	public void setAccount_number(String accountNumber) {
		account_number = accountNumber;
	}

	@Transient
	public String getRoleName() {
		for (Role role : roles) {
			String role_name =  role.getDescn();
			setRoleName(role_name);
		}
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
}
